import React from 'react';
import PropTypes from 'prop-types';
import ShowMoreList from '@components/show-more-list';
import { transDeviceName } from '@util/trans-device-name';
import { connect } from 'react-redux';
import _ from 'lodash';

/* eslint-disable  camelcase */
export const subKeysMap = {
  location: '地理位置',
  os: '操作系统',
  port: '端口',
  service: '服务',
  component: '组件',
  device: '设备类型',
  web_language: '开发语言',
  web_app: 'Web应用',
  web_server: 'Web容器',
  web_framwork: 'Web框架',
  web_db: '数据库',
  web_waf: '防护信息'
};

const limitsMap = {
  location: 5,
  os: 5,
  port: 5,
  service: 5,
  component: 5,
  device: 5,
  web_language: 5,
  web_app: 5,
  web_server: 5,
  web_framwork: 5,
  web_db: 5,
  web_waf: 5
};
/* eslint-enable */

const FilterList = props => {
  const { data, onSelect, scanType = 'Zoomeye', suffix = '_bar', modules = [] } = props;
  let subKeys = ['os', 'device', 'port', 'service', 'component'];
  if (scanType === 'Zoomeye') {
    subKeys = ['os', 'device', 'port', 'service', 'component'];
    if (_.includes(modules, 'ip-location')) {
      subKeys.unshift('location');
    }
  } else if (scanType === 'Web') {
    subKeys = ['web_app', 'web_server', 'web_framwork', 'web_waf', 'web_db', 'web_language'];
  }
  const items = subKeys.map(key => {
    const title = subKeysMap[key];
    const limit = limitsMap[key];
    let children = _.get(data, `${key}${suffix}`, []);
    if (key === 'device') {
      children = transDeviceName(_.get(data, `${key}${suffix}`, []));
    }
    return {
      title,
      key,
      limit,
      children,
      fieldNames: { label: 'name', value: 'count' },
      mode: 'double'
    };
  });

  return (
    <div className="tmd-filer-list">
      <ShowMoreList items={items} onSelect={onSelect} />
    </div>
  );
};

FilterList.propTypes = {
  scanType: PropTypes.object,
  data: PropTypes.object,
  onSelect: PropTypes.func,
  suffix: PropTypes.string,
  modules: PropTypes.arrayOf(PropTypes.string)
};

const mapStateToProps = state => {
  return {
    modules: _.get(state, 'auth.cert.modules', [])
  };
};

export default connect(
  mapStateToProps,
  null
)(FilterList);
